package controller;

import domain.flower;
import execption.repeatValueException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import utill.ParamUtil;
import utill.component.promptBox;
import utill.switchpages;
import service.*;
import service.impl.*;

public class BreedFlowerController {

    @FXML
    private Label GLabelprice;

    @FXML
    private HBox TitleHbox;

    @FXML
    private Label GLabelTotal;

    @FXML
    private TextField GTextStroeNumber;

    @FXML
    private Button btnClear;

    @FXML
    private TextField GTextType;

    @FXML
    private MenuBar menuBar;

    @FXML
    private Label GLabelType;

    @FXML
    private Label GLabelStromeNumber;

    @FXML
    private TextField GTextPrice;

    @FXML
    private Menu MenuBack;

    @FXML
    private Label TitleLabel;

    @FXML
    private Menu MenuSignOut;

    @FXML
    private GridPane GridFlowerPane;

    @FXML
    private TextField GTextNumber;

    @FXML
    private HBox bottomHbox;

    @FXML
    private Button btnCommit;


    @FXML
    private AnchorPane ParentPane;

    String lastStepUrl="/controller/storeManage.fxml";

    String signOutUrl="/controller/login.fxml";

    FlowerStoreService flowerStoreService =new FlowerStoreServiceImpl();



    @FXML
    void callback(ActionEvent event) throws Exception {
        switchpages switchpage =new switchpages();
        switchpage.switch_viewPane(this.ParentPane,this.lastStepUrl);
    }

    @FXML
    void signout(ActionEvent event) throws Exception {
        //清理静态map中的数据
        ParamUtil.storeId.remove(Thread.currentThread()+"storeId");
        switchpages switchpage =new switchpages();
        switchpage.switch_viewPane(this.ParentPane,this.signOutUrl);
    }

    @FXML
    void clearContext(ActionEvent event) {
       clear();
    }

    @FXML
    void Commit(ActionEvent event) {
       String type=GTextType.getText();
       String number=GTextNumber.getText();
       String price =GTextPrice.getText();
       String storeId=GTextStroeNumber.getText();

       if("".equals(type) || "".equals(number) || "".equals(price) || "".equals(storeId)){
           promptBox.tipsMessage("不能出现值为空的列");
       }

        flower f =new flower();
        f.setFlowerType(type);
        f.setFlowerPrice(price);
        f.setFlowertotal(Integer.valueOf(number));
        f.setStoreId(Integer.valueOf(storeId));
        boolean flag=false;
        try {
             flag =flowerStoreService.AddFlowers(f);
        } catch (repeatValueException e) {
            promptBox.tipsMessage(e.getMessage());
            e.printStackTrace();
        }finally {
            if(flag) {
                promptBox.tipsMessage("添加成功");
            }else {
                promptBox.tipsMessage("添加失败");
            }
        }
        clear();
    }

    public void clear(){
        GTextType.setText("");
        GTextNumber.setText("");
        GTextPrice.setText("");
        GTextStroeNumber.setText("");
    }
}
